<template>
  <div>
    <el-card>
      <el-table :data="skuList.records" style="width: 100%" border>
        <el-table-column type='index' label="序号" width="80" align="center">
        </el-table-column>
        <el-table-column prop="skuName" label="名称" width="width" align="center">
        </el-table-column>
        <el-table-column prop="skuDesc" label="描述" width="width" align="center">
        </el-table-column>
        <el-table-column label="默认图片" width="110" align="center">
          <template slot-scope="{row}">
            <img :src="row.skuDefaultImg" alt="" style="width:100px;height:100px">
          </template>
        </el-table-column>
        <el-table-column prop="weight" label="重量" width="120" align="center">
        </el-table-column>
        <el-table-column prop="price" label="价格" width="120" align="center">
        </el-table-column>
        <el-table-column label="操作" width="width" align="center">
          <template slot-scope="{row}">
            <el-button type="info" icon="el-icon-download" size="mini" v-if="row.isSale==1" @click="cancelSale(row)"></el-button>
            <el-button type="success" icon="el-icon-upload2" size="mini" v-else @click="onSale(row)"></el-button>
            <el-button type='primary' icon="el-icon-edit" size="mini" @click="edit"></el-button>
            <el-button type="info" icon="el-icon-info" size="mini" @click="ShowInfo(row)"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteSku(row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="getSkuList" :current-page="page" :page-sizes="[5, 10, 20]" :page-size="limit" layout=" prev, pager, next, jumper, ->, sizes,total" :total="skuList.total" style="text-align:center">
      </el-pagination>
    </el-card>

    <el-drawer :visible.sync="show" :show-close="false" size="50%">
      <el-row>
        <el-col :span="5">名称</el-col>
        <el-col :span="16">{{skuInfo.skuName}}</el-col>
      </el-row>

      <el-row>
        <el-col :span="5">描述</el-col>
        <el-col :span="16">{{skuInfo.skuDesc}}</el-col>
      </el-row>

      <el-row>
        <el-col :span="5">价格</el-col>
        <el-col :span="16">{{skuInfo.price}}</el-col>
      </el-row>

      <el-row>
        <el-col :span="5">平台属性</el-col>
        <el-col :span="16">
          <template>
            <el-tag type="success" style="margin:0 5px" v-for="item in skuInfo.skuAttrValueList" :key="item.id">{{item.attrId}}--{{item.valueId}}</el-tag>
          </template>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="5">商品图片</el-col>
        <el-col :span="16">
          <el-carousel :interval="5000" arrow="always">
            <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
              <img :src="item.imgUrl" alt="">
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: "Sku",
  data() {
    return {
      page: 1,
      limit: 5,
      skuList: {},
      show: false,
      skuInfo: {},
    };
  },
  created() {
    this.getSkuList();
  },
  methods: {
    async getSkuList(pages = 1) {
      this.page = pages;
      const result = await this.$API.sku.reqSkuList(this.page, this.limit);
      if (result.code == 200) {
        this.skuList = result.data;
      }
    },
    handleSizeChange(value) {
      this.limit = value;
      this.getSkuList();
    },
    async onSale(row) {
      const result = await this.$API.sku.reqOnSale(row.id);
      console.log(row);
      if (result.code == 200) {
        this.$message({
          type: "success",
          message: "上架成功",
        });
        row.isSale = 1;
      }
    },
    async cancelSale(row) {
      const result = await this.$API.sku.reqCancelSale(row.id);
      console.log(row);
      if (result.code == 200) {
        this.$message({
          type: "success",
          message: "下架成功",
        });
        row.isSale = 0;
      }
    },
    deleteSku(row) {
      this.$confirm(`确认删除${row.skuName}?`, "提示", {
        cancelButtonText: "取消",
        confirmButtonText: "确定",
        type: "warning",
      }).then(async () => {
        const result = await this.$API.sku.reqDeleteSku(row.id);
        if (result.code == 200) {
          this.$message({
            type: "success",
            message: "删除成功",
          });
          this.getSkuList();
        }
      });
    },
    edit() {
      this.$message({
        type: "info",
        message: "功能开发中",
      });
    },
    async ShowInfo(row) {
      this.show = true;
      const result = await this.$API.sku.reqSkuById(row.id);
      if (result.code == 200) {
        this.skuInfo = result.data;
      }
    },
  },
};
</script>

<style scoped>
.el-col {
  margin: 10px 10px;
}
.el-col-5 {
  text-align: right;
  font-size: 20px;
}

.el-carousel__item h3 {
  color: #475669;
  font-size: 18px;
  opacity: 0.75;
  line-height: 300px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>